package com.xfatm.util.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * 依赖于jedis
 */
public class JedisUtils {
    private static JedisPool jedisPool;

    private static JedisPool getJedisPool(){
        if(jedisPool==null){
            synchronized (JedisUtils.class){
                if(jedisPool==null){
                    jedisPool=new JedisPool("192.168.75.128",6379);
                }
            }
        }
        return jedisPool;
    }

    /**
     * 获取Jedis
     * @return 返回null则获取redis失败
     */
    public static Jedis getJedis(){
        try{
            return getJedisPool().getResource();
        }catch (JedisConnectionException e){
            System.out.println("获取jedis失败");
            if(jedisPool!=null){
                jedisPool.close();
                jedisPool=null;
            }
        }
        return null;
    }

}
